<?php

namespace App\Admin\Controllers;

use App\Admin\Filters\TimestampBetween;
use App\Models\Banner;
use App\Models\Benefit;
use App\Models\Campus;
use App\Models\Goods;
use App\Models\Shop;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class GoodsController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        'off' => ['value' => 1, 'text' => '正常', 'color' => 'default'],
    ];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('兑换商品列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('兑换商品详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑兑换商品')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增兑换商品')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '标题');
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->use(new TimestampBetween('created_at', __('created_at')))->datetime();
            });
        });
//
        $grid->column('id', 'ID')->sortable();
        $grid->column('title', '标题')->sortable();
        $grid->column('shopInfo.name','店铺');
        $grid->column('score','积分')->sortable();
        $grid->column('worth','价值')->sortable();
        $grid->column('qty','库存')->sortable();
        $grid->column('pics', '图片')->lightbox(['width' => 60, 'height' => 60]);
        $grid->column('status', '状态')->switch(self::INVITE_STATUS);
        $grid->column('created_at', __('created_at'));

        $grid->actions(function ($actions) {
            $actions->disableView();

        });
        Admin::style('.img-thumbnail {width: 60px;height:60px}');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Banner::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Goods());
        $form->select('shop_id', '店铺')->options(Shop::where('status', 1)->pluck('name as text', 'id'))->required();
        $form->text('title', '商品名称')->required();
        $form->number('score', '需要积分')->required()->default(0);
        $form->number('worth', '价值')->default(0);
        $form->number('qty', '库存')->default(0)->min(0)->required();
//        $form->keyValue('reflex', '兑换条件');
        $form->multipleImage('pics', '商品图片')->uniqueName()->removable()->rules(function (Form $form) {
            // 如果是编辑模式且没有修改图片，去掉 required 校验
            if ( empty($form->model()->pics)) {
                return 'required';  // 只在没有图片时强制要求上传
            }
        },['required'=>'请上传图片']);
        $form->radio('status', '状态')->options([0 => '禁用', 1 => '正常'])->default(1);
        $form->editor('rule', '规则');
        $form->editor('content', '内容');
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

}
